package org.example.order.controller;

import org.example.order.model.OrderInfo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.UUID;

@RequestMapping("/order")
@RestController
public class OrderController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @RequestMapping("/create")
    public String create() {
        //参数校验，数据库保存等，业务代码
        //发送消息
        String orderId= UUID.randomUUID().toString();
        rabbitTemplate.convertAndSend("", "order.create", "订单信息，订单ID"+orderId);
        return "下单成功";
    }
    @RequestMapping("/create2")
    public String create2( ) {
        //这里进行发送一个对象给消息队列中
        OrderInfo orderInfo = new OrderInfo();
        //生成一个100以内的数字
        orderInfo.setOrderName("商品"+new Random().nextInt(100));
        orderInfo.setOrderId(UUID.randomUUID().toString());
        //在给消息队列发送消息的时候，默认的类型是一个String类型的
        //但是这里要发送对象的时候，就需要进行写配置
        System.out.println(orderInfo);
        rabbitTemplate.convertAndSend("", "order.create",orderInfo);
        return "下单成功2";
    }
}
